import java.util.LinkedList;
import java.util.List;

public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null){
            return null;
        }
        ListNode ps = null;
        ListNode pe = null; //前半部分头尾指针
        ListNode as = null;
        ListNode ae = null;//后半部分头尾指针
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                if(ps == null){
                    ps = pe = cur;
                }else{
                    pe.next = cur;
                    pe = cur;
                }
            }else{
                if(as == null){
                    as = ae = cur;
                }else{
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        if(ps == null){
            return as;
        }
        if(as == null){
            return ps;
        }
        pe.next = as;
        return ps;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode();
        listNode.addLast(25);
        listNode.addLast(11);
        listNode.addLast(3);
        listNode.addLast(1);

        Partition partitions = new Partition();
        partitions.partition(listNode.head, 10);
        System.out.println("hehe");
        List<Integer> list = new LinkedList<>();
        
    }
}